package my;

import log.controller.ControllerLogger;
import log.controller.Invocation;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Component
public class MyControllerLogger implements ControllerLogger {
/** 需要记录日志的方法列表 */
    private volatile List<String> methods = Collections.unmodifiableList(new LinkedList<>());

    public void log(Invocation invocation) throws Exception {
        Method method = invocation.getMethod();
        if (!methods.contains(method.getName())) {
            //            return;
        }
        long startTime = invocation.getStartTime();
        long cost = System.currentTimeMillis() - startTime;

        Object[] args = invocation.getArgs();
        Class controllerClass = method.getDeclaringClass();
        String className = controllerClass.getName();

        System.out.println(className + "." + method.getName() + "() - " + cost + " ms - " + Arrays.toString(args));
    }
    public void setMethods(List<String> methods) {
        this.methods = Collections.unmodifiableList(new LinkedList<>(methods));
    }
}
